package org.jsping.service.impl;

import java.sql.SQLException;

import org.jsping.dao.impl.UserDaoImpl;
import org.jsping.entity.User;
import org.jsping.service.ILoginUserService;

public class LoginUserServiceImpl implements ILoginUserService {
	//User表操作类
	private UserDaoImpl userDao = new UserDaoImpl();
	
	@Override
	public User login(String user, String pwd) {
		try {
			return userDao.getAUser(user, pwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			userDao.releaseAll();
		}
		return null;
	}
	
	//增加一个qq或者微信用户
	@Override
	public boolean addOpenidUser(User u) {
		//首先需要判断一下这个用户存不存在
		User user = null;
		try {
			user = userDao.getAUser(u.getOpenid());
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			userDao.releaseAll();
		}
		System.out.println(u.getOpenid());
		//如果不存在那么就添加
		if (user==null) {
			try {
				return userDao.addOpenUser(u);
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				userDao.releaseAll();
			}
		}
		return false;
	}
	
	@Override
	public User obtain(String openid) {
		return userDao.obtain(openid);
	}
}
